package com.woniuxy.ptapp.app.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.util.Result;
import com.woniuxy.ptapp.infra.model.ClassesOrder;
import com.woniuxy.ptapp.service.ClassesOrderService;
import com.woniuxy.relation.client.AnnouncementClient;
import com.woniuxy.relation.client.form.SendAnnouncementForm;
import com.woniuxy.userapp.client.CoachClient;
import com.woniuxy.userapp.client.VenueClient;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RocketMQMessageListener(consumerGroup = "TakeOrderTimeout",topic = "Orders")
public class TakeOrdersListener implements RocketMQListener {
    @Resource
    ClassesOrderService classesOrderService;
    @Resource
    AnnouncementClient ac;
    @Resource
    StringRedisTemplate srt;
    @Resource
    VenueClient venueClient;
    @Resource
    CoachClient coachClientl;

    @Override
    public void onMessage(Object o) {
        //消费者幂等性
        String s = srt.opsForValue().get("yoga-pt:TackOrder"+o);
        if(s!=null){
            if(s.equals("exist")){
                System.out.println("检查重复消费,结束方法==========");
                return;
            }
        }
        srt.opsForValue().set("yoga-pt:TackOrder"+o,"exist");
        System.out.println("进入消费者消费orderState");
        //通过orderid查询订单
        QueryWrapper<ClassesOrder> qw = new QueryWrapper<>();
        qw.eq("id",o);
        ClassesOrder one = classesOrderService.getOne(qw);
        //获取场馆账号id
        Result<Integer> accountIdByVenueId = venueClient.findAccountIdByVenueId(one.getVenueId());
        //获取场馆教练id
        Result<Integer> accountIdByCoachId = coachClientl.findAccountIdByCoachId(one.getCoachId());
        //判断场馆（订单状态还是已下单）是否接单
        if(one.getState()==12){
            //需要系统发送通知告诉用户场馆超时未接单已经取消预约
            SendAnnouncementForm sendAnnouncementForm = new SendAnnouncementForm();
            sendAnnouncementForm.setAcceptAccountId(accountIdByVenueId.getData()+"");
            sendAnnouncementForm.setContent("尊敬的"+one.getVenueName()+"您好,由于您超时未接单,考虑用户体验,系统自动取消订单,请知晓");
            ac.sendAnnouncement(sendAnnouncementForm);
            one.setState(5);
            classesOrderService.saveOrUpdate(one);
            //判断教练是否接单(如果两个小时之后状态还是场馆接单代表教练没有接单 订单状态改为取消)
        }else if(one.getState()==2){
            //教练超时未接单,通知场馆,通知教练
            SendAnnouncementForm sendAnnouncementForm = new SendAnnouncementForm();
            sendAnnouncementForm.setAcceptAccountId(accountIdByVenueId.getData()+"");
            sendAnnouncementForm.setContent("尊敬的"+one.getVenueName()+"您好,由于您旗下教练"+one.getCoachName()+"超时未接单,考虑用户体验,系统自动取消订单,请知晓");
            ac.sendAnnouncement(sendAnnouncementForm);
            SendAnnouncementForm sendAnnouncementForm1 = new SendAnnouncementForm();
            sendAnnouncementForm1.setAcceptAccountId(accountIdByCoachId.getData()+"");
            sendAnnouncementForm1.setContent("尊敬的"+one.getCoachName()+"您好,由于您超时未接单,考虑用户体验,系统自动取消订单,请知晓");
            ac.sendAnnouncement(sendAnnouncementForm1);
            one.setState(5);
            classesOrderService.saveOrUpdate(one);
        }

    }
}
